package com.zeeephr.core;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zeeephr.core.bean.Conf;


//跟tomcat中的不同 这里写的Request中将保存传送过来的信息
//假设发送过来的请求头为 
public class Request {
	private Map<String,List<String>> map;
	private String allStr;
	private String requestHead; 
	private String uri;
	private String url;
	private String method;
	private String kvStr;
	private String postKVStr;
	public String getRequestHead() {
		return requestHead;
	}
	public String getUri() {
		return uri;
	}
	public String getUrl() {
		return url;
	}
	public String getMethod() {
		return method;
	}
	public Request(InputStream input) throws IOException {
		
		//BufferedReader reader=new BufferedReader(new InputStreamReader(input));
		//requestHead=reader.readLine();
		init(input);
		show();
		
	}
	private void init(InputStream input) throws IOException {
		System.out.println("初始化Request");
		byte flush[]=new byte[1024*Server.conf.getSize()];
		int len=0;
		len=input.read(flush);
		allStr=new String(flush,0,len,"UTF-8");
		System.out.println("----------------request------------------");
		System.out.println(allStr);
		System.out.println("----------------request------------------");
		requestHead=allStr.substring(0, allStr.indexOf("\r\n")).trim();
		getMethodAndUri();
		getUrlAndKV();
		if("post".equals(method)) {
			postKVStr=allStr.substring(allStr.lastIndexOf("\r\n"));
			kvStr=kvStr==null?"":kvStr+"&";
			kvStr+=postKVStr;
		}
		if(kvStr!=null&&kvStr.length()>0) {
			setKV();
		}
	}
	
	private void getMethodAndUri() {
		int index1=requestHead.indexOf("/");
		int index2=requestHead.indexOf("HTTP/");
		method=requestHead.substring(0,index1).toUpperCase().trim();
		uri=requestHead.substring(index1,index2);
	}
	private void getUrlAndKV() {
		if(uri.length()>0) {
			int index=uri.indexOf("?");
			if(index>=0) {//可能没有？ 此时index 为-1
				url=uri.substring(0,index).trim();
				kvStr=uri.substring(index+1);
			}
		}
	}
	public void show() {
		System.out.println("-----------splitMessage--------------");
		System.out.println("requestHead:\n"+requestHead);
		System.out.println("method:\n"+method);
		System.out.println("uri:\n"+uri);
		System.out.println("uri len:\n"+ uri.length());
		System.out.println("url:\n"+url);
		System.out.println("kvStr:\n"+kvStr);
		System.out.println("postKVStr:\n"+postKVStr);
		System.out.println("-----------splitMessage--------------");
		
	}
	private void setKV() {
		if(kvStr.length()>0) {
			map=new HashMap<String,List<String>>();
			String kvs[]=kvStr.split("&");
			for(int i="POST".equals(method)?1:1;i<kvs.length;i++) {
				String kav[]=kvs[i].split("=");
				kav=Arrays.copyOf(kav, 2);
				System.out.println(kav[0]+"---》"+kav[1]);
				if(!map.containsKey(kav[0])) {
					map.put(kav[0], new ArrayList<String>());
				}
				map.get(kav[0]).add(kav[1]);
			}
		}
	}
	
	public String[] getParameterValues(String key) {
		if(map==null||map.size()<1)return null;
		else{
			ArrayList<String> list=(ArrayList<String>) map.get(key);
			if(list==null||list.size()<1)return null;
			return list.toArray(new String[0]);
		}
	}
	public String getParameter(String key) {
		String res[]=getParameterValues(key);
		return res==null?null:res[0];
	}
}
